#include "gamewindow.h"
#include "ui_gamewindow.h"

GameWindow::GameWindow(bool alone, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GameWindow)
{
    playdesk = new Playdesk(alone);
    connect(playdesk,SIGNAL(ReDraw()),SLOT(Draw_Slot()));
    connect(playdesk,SIGNAL(PaintRing(mline)),SLOT(Ring_Slot(mline)));
    ui->setupUi(this);
    line = new mline [2000];
    for (int i = 0; i < 2000; i++)
        line[i].kol=0;

    line_pen = new QPen(Qt::black, 2);
    null_pen = new QPen(Qt::red, 0);
    _brush = new QBrush(Qt::white,Qt::Dense4Pattern);
    scene = new QGraphicsScene;
    ui->graphicsView_goban->setScene(scene);
    ui->graphicsView_goban->setRenderHint(QPainter::Antialiasing);
    ui->graphicsView_goban->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    ui->graphicsView_goban->show();
}

void GameWindow::Ring_Slot(mline m)
{
    static int num_ap=0;
    int i=0;
    //line[num_ap]=m;
    line[num_ap].kol = m.kol;
    line[num_ap].color = m.color;
    //if(line[num_ap].line)delete line[num_ap].line;
    line[num_ap].line = new mpoint[line[num_ap].kol+1];
    for(int i=0;i<line[num_ap].kol+1;i++)
        line[num_ap].line[i]=m.line[i];

    //delete b.line;
    num_ap++;
}

GameWindow::~GameWindow()
{
    delete ui;
}

void GameWindow::Draw_Slot()
{
    scene->clear();
    QPen *lpen;
    int SizeMin;
    if (ui->graphicsView_goban->size().width() > ui->graphicsView_goban->size().height())
        SizeMin = ui->graphicsView_goban->size().height();
        else SizeMin = ui->graphicsView_goban->size().width();

    double step = ((double)SizeMin) / 20;
    //Point_Graph::stepp = step;

    scene->addRect(0,0,SizeMin,SizeMin,*(this->null_pen),*(this->_brush));

    for (int i=1; i<20; i++)
    {
        scene->addLine(i*step, 1*step, i*step, 19*step, *(this->line_pen));
        scene->addLine(1*step, i*step, 19*step, i*step, *(this->line_pen));
    }

    for (int i = 0; i < 19; i++)
        for (int j = 0; j < 19; j++)
        {
            QPoint point(i,j);
            Point_Graph *ppoint;
            if (playdesk->GetPoint(point) == None) ppoint = new Point_Graph(step,None,playdesk,i,j);
            if (playdesk->GetPoint(point) == Blue) ppoint = new Point_Graph(step,Blue,playdesk,i,j);
            if (playdesk->GetPoint(point) == Red) ppoint = new Point_Graph(step,Red,playdesk,i,j);
            ppoint->setPos((i+1)*step,(j+1)*step);
            scene->addItem(ppoint);
        }
    for(int i=0;line[i].kol;i++)
        for(int j=0;j<line[i].kol;j++)
            if((line[i].line[j+1].x==0 && line[i].line[j+1].y!=0) || (line[i].line[j+1].x!=0 && line[i].line[j+1].y==0)|| (line[i].line[j+1].x!=0 && line[i].line[j+1].y!=0))
            {
                if(line[i].color==2)
                    lpen=new QPen(Qt::red, 2);
                if(line[i].color==1)
                    lpen=new QPen(Qt::blue, 2);
                scene->addLine((line[i].line[j].x+1)*step,(line[i].line[j].y+1)*step,(line[i].line[j+1].x+1)*step,(line[i].line[j+1].y+1)*step,*(lpen));
            }
}

void GameWindow::on_pushButton_exit_pressed()
{
    this->close();
}

void GameWindow::on_pushButton_pass_pressed()
{
    playdesk->Pass();
}

void GameWindow::resizeEvent(QResizeEvent *)
{
    Draw_Slot();
}

void GameWindow::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    Draw_Slot();
}

void GameWindow::on_pushButton_clicked()
{
    /*int red=0,blue=0;
    for (int i = 0; i < 19; i++)
        for (int j = 0; j < 19; j++)
        {
            if(playdesk->matr[i][j].color==1 && playdesk->matr[i][j].status==5)
                blue++;
            if(playdesk->matr[i][j].color==2 && playdesk->matr[i][j].status==5)
                red++;
        }
    QMessageBox msg;
    if(red>blue)
        msg.setText("Blue Win!");
    if(red<blue)
        msg.setText("Red Win!");
    msg.exec();
    this->close();*/


}
